package org.springsource.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonManagedReference;

@Entity
@Table(name = "Account")
//@JsonIgnoreProperties("accountCategories")
public class Account implements Serializable{


	/**
	 * 
	 */
	private static final long serialVersionUID = 8639465507320717821L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(nullable=false)
	private Long id;

	@NotNull
	private String name;
	
	@Size(min=2,max=40)
	@Pattern(regexp = "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})", message = "bad req")
	private String email;
	
	
	@JsonManagedReference("cars")
	@OneToMany(fetch = FetchType.EAGER,cascade={CascadeType.ALL})
	@JoinColumn(name="account_id")
    private List<Car> cars;
	
	@JsonIgnore
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.account", cascade=CascadeType.ALL)
	private Set<AccountCategory> accountCategories = new HashSet<AccountCategory>(0);
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Account() {
	}

	public Account(Long id, String name) {
		this.id = id;
		this.name = name;
	}


	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toString() {
		return "[Account id = '" + id + "', name = " + name + "Cars = "+ "']";
	}

	public List<Car> getCars() {
		return cars;
	}

	public void setCars(List<Car> cars) {
		this.cars = cars;
	}
	//@JsonManagedReference("categories")
	
	public Set<AccountCategory> getAccountCategories() {
		return accountCategories;
	}

	public void setAccountCategories(Set<AccountCategory> accountCategories) {
		this.accountCategories = accountCategories;
	}

}
